import React from 'react'
import { observable, action, toJS, computed } from 'mobx';
import { Tools, Popover } from 'weaver-mobile'
import { SearchAdvanced, ListAdvanced, RouteLayout, Browser, BrowserHrm, Upload } from 'weaver-mobile-page';
import { Modal, Toast, Bubble } from 'weaver-mobile'
import * as TaskMainApis from '../../api/task/taskMain'
import TaskCommonStore from '../../stores/task/TaskCommonStore'
const taskCommonStore = new TaskCommonStore()
const getLabel = Tools.getLabel;

export default class TaskDetailStore {

  @observable
  tabs = [
    { title: getLabel(1361, '基本信息') },
    { title: getLabel(83802, '下级任务') },
    { title: getLabel(22078, '相关信息') },
    { title: getLabel(83596, '反馈信息') },
    { title: getLabel(32061, '操作日志') }
  ]

  @observable
  taskid = ""

  btnArray = {
    "1": { name: getLabel(382564, '标记完成'), type: "complete" },
    "2": { name: getLabel(382565, '标记进行'), type: "doing" },
    "4": { name: getLabel(515068, '待确认'), type: "" },
    "5": { name: getLabel(514155, '重发'), type: "resend", key: "102" },
    "6": {name: getLabel(19134, '审批中'), type: ""}
  }

  infoArray = {
    "complete":getLabel(508674, '确认要标记此任务为完成吗？'),
    "doing":getLabel(508676, '确认要标记此任务为进行中吗？'),
    "delete":getLabel(508794, '确认要删除此任务吗？'),
    "confirm":getLabel(515080,'确认要接收此任务吗？'),
    "resend":getLabel(515097,'确认要重新向责任人发送此任务吗？'),
    "audit":getLabel(515165,'确认要同意任务标记完成？'),
    "sendback":getLabel(515166,'确认要拒绝任务标记完成？')
  }

  @action
  changeTaskid = (taskid) => {
    this.taskid = taskid
  }

  // @observable
  // showHeadPopover = false

  // @action
  // changeShowHeadPopover = (showHeadPopover) => {
  //   this.showHeadPopover = showHeadPopover
  // }

  @observable
  taskDetail = {}

  @action
  changeTaskDetail = (taskDetail) => {
    this.taskDetail = taskDetail
  }

  @action
  changeTaskDetailField = (fieldname, fieldvalue) => {
    // this.taskDetail[fieldname] = fieldvalue
    this.taskDetail = { ...this.taskDetail, [fieldname]: fieldvalue }
  }

  @computed
  get headPopoverlay() {
    let itemStyle = {
      minWidth: "90px",
      textAlign: "center"
    }
    let buttons = []
    let _isconfirm = this.taskDetail.isconfirm;
    if (_isconfirm) {
      buttons.push(<Popover.Item key="100" value={"confirm"} style={itemStyle}>{getLabel(18526, '接收')}</Popover.Item>)
      buttons.push(<Popover.Item key="5" value={"refuse"} style={itemStyle}>{getLabel(25659, '拒绝')}</Popover.Item>)
    } else {
      buttons.push(<Popover.Item key="1" value={this.taskDetail.special == 1 ? "cancelSpecial" : "special"} style={itemStyle}>{this.taskDetail.special == 1 ? getLabel(503684, '取消关注') : getLabel(504445, '关注')}</Popover.Item>)
      if (this.taskDetail != null) {
        const {canedit,canaudit,candel,canremind,taskstatus} = this.taskDetail;
        if (canedit && !canaudit) {
          let _obj = this.btnArray[taskstatus];
          if (_obj) {
            buttons.push(<Popover.Item key={_obj.key ? _obj.key : "2"} value={_obj.type} style={itemStyle}>{_obj.name}</Popover.Item>)
          }
        }
        if(canaudit){
          buttons.push(<Popover.Item key="104" value={"audit"} style={itemStyle}>{getLabel(142, '批准')}</Popover.Item>)
          buttons.push(<Popover.Item key="105" value={"sendback"} style={itemStyle}>{getLabel(236, '退回')}</Popover.Item>)
        }
        if (candel) {
          buttons.push(<Popover.Item key="3" value={"delete"} style={itemStyle}>{getLabel(91, '删除')}</Popover.Item>)
        }
        if(canremind){
          buttons.push(<Popover.Item key="103" value={"urgetask"} style={itemStyle}>{getLabel(23756, '催办')}</Popover.Item>)
        }
      }
      this.currpage != 3 && buttons.push(<Popover.Item key="4" value={"addfb"} style={itemStyle}>{getLabel(21950, '反馈')}</Popover.Item>)
    }
    return buttons
  }

  @observable
  currpage = 0

  @action
  changeCurrpage = (currpage) => {
    this.currpage = currpage
  }

  @observable
  commentReplayAutoFocus = "0"

  @action
  changeCommentReplayAutoFocus = (commentReplayAutoFocus) => {
    this.commentReplayAutoFocus = commentReplayAutoFocus
  }

  @observable
  errormsg = ""

  @action
  changeErrormsg = (errormsg) => {
    this.errormsg = errormsg
  }
  @action("任务详情初始化")
  initTaskForm = (params) => {
    Toast.loading('Loading...', 0);
    params.taskId = this.taskid;
    TaskMainApis.getTaskDetail(params).then(res => {
      Toast.hide();
      if (res && res.status == "1") {
        res.id = this.taskid;
        this.changeTaskDetail(res);
      } else {
        this.changeErrormsg((res && res.errormsg) || "Interface Error");
      }
    })
  }
  @action("任务状态变化")
  taskChangeStatus = (params) => {
    params.taskId = this.taskid;
    Modal.alert(getLabel('386786', '信息确认'), params.infos, [
      { text: getLabel(201, '取消'), style: 'default' },
      {
        text: getLabel('826', '确定'),
        onPress: () => {
          TaskMainApis.changeTaskStatus(params).then(res => {
            if (res.status == "1") {
              Bubble.success(getLabel(30700, '操作成功!'));
              if (params.status == 100 || params.status == 104 || params.status == 105 || params.status == 2) {//接收任务 批准 退回
                this.initTaskForm({});
              }
              if (params.status == 102) {//重发任务
                this.changeTaskDetailField("taskstatus", "4")
              }
              if (params.doback) {
                params.doback(res.newstatus);
              }
            }
          })
        }
      }
    ])
  }

  @action("任务关注变化")
  taskChangeSpecial = (params) => {
    params.taskid = this.taskid
    TaskMainApis.setSpecial(params).then(res => {
      if (res.status == "1") {
        this.changeTaskDetailField("special", params.special == "0" ? 1 : 0)
        if (params.special == "0") {
          taskCommonStore.attentionSuccess()
        } else {
          taskCommonStore.cancelAttentionSuccess()
        }
      }
    })
  }
}